package com.atguigu.gulimallcart.controller;

import com.atguigu.gulimallcart.service.CartService;
import com.atguigu.gulimallcart.vo.CartItemVo;
import com.atguigu.gulimallcart.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Resource
    private CartService cartService;
    @GetMapping("/cartList.html")
    public String cartList(){
        return "cartList";
    }

    @GetMapping("/addToCart")
    public String addToCart(Long skuId, int num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItemVo cartItemVo=cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com:30000/success.html";
    }

    @GetMapping("/success.html")
    public String success(@RequestParam("skuId")Long skuId,Model model){
        CartItemVo cartItemVo=cartService.getCarSkuInfo(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }

    @GetMapping("/myCart")
    public String myCart(Model model) throws ExecutionException, InterruptedException {
        CartVo cartVo=cartService.myCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVo> currentUserCartItems(){
        List<CartItemVo> cartItemVos =cartService.getUserCartItems();
        return cartItemVos;
    }

    @GetMapping("deleteCart")
    public String deleteCart(@RequestParam("skuId")Long skuId){
        cartService.deleteCart(skuId);
        return "redirect:http://cart.gulimall.com:30000/myCart";
    }

}
